package com.rain.shoppingscienceproj.ui.activity.order;

import android.os.Bundle;
import android.widget.ImageView;

import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.http.BaseSubscriber;
import com.rain.shoppingscienceproj.http.ExceptionHandle;
import com.rain.shoppingscienceproj.http.RetrofitClient;
import com.rain.shoppingscienceproj.ui.activity.BaseActivity;
import com.rain.shoppingscienceproj.ui.activity.pay.PayActivity;
import com.rain.shoppingscienceproj.ui.base.GlobalData;
import com.rain.shoppingscienceproj.ui.utile.DialogUtils;
import com.rain.shoppingscienceproj.ui.utile.TitleUtils;
import com.rain.shoppingscienceproj.utils.AppManager;
import com.rain.shoppingscienceproj.utils.LoadingDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;

public class FinishPayOrderActivity extends BaseActivity {

    private int type = -1;
    @BindView(R.id.iv_image)
    ImageView iv_image;
    private String orderId = "";
    private int status = -1;

    @OnClick(R.id.tv_sure)
    void sure() {
        finish();
        if (status == 2) {
            AppManager.getAppManager().finishActivity(PayActivity.class);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finish_pay_order);
        ButterKnife.bind(this);
        AppManager.getAppManager().addActivity(this);
        TitleUtils.TitleView(this, "支付结果");
        type = getIntent().getIntExtra("type", -1);
        userInfo = GlobalData.getUserInfo();
        loadingDialog = new LoadingDialog(this, "正在加载...");
        orderId = GlobalData.getOrderId();
        status = GlobalData.getStatus();
        initData();
    }

    private void initData() {
        loadingDialog.show();
        RetrofitClient.getInstance(this)
                .createBaseApi()
                .queryorderbystatus(orderId, new BaseSubscriber<ResponseBody>(this) {
                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable e) {
                        loadingDialog.dismiss();
                        if (type == 0) {
                            iv_image.setImageResource(R.drawable.pay_error);
                            DialogUtils.dialogRegis(FinishPayOrderActivity.this, "网络异常，请联系店铺客服管理员");
                            DialogUtils.getTv_sure().setOnClickListener(view -> {
                                DialogUtils.dismissDialog();
                                FinishPayOrderActivity.this.finish();
                            });
                        }
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        super.onNext(responseBody);
                        loadingDialog.dismiss();
                        try {
                            String request = responseBody.string();
                            JSONObject json = new JSONObject(request);
                            int status = json.getInt("code");
                            GlobalData.setOrderId("");
                            if (status == 0) {
                                iv_image.setImageResource(R.drawable.pay_success);
                            } else {
                                iv_image.setImageResource(R.drawable.pay_error);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
